package com.hunter.applearn.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import com.hunter.applearn.api.Api
import com.hunter.applearn.api.ApiConfig.Companion.POST
import com.hunter.applearn.api.TtitCallBack


import com.hunter.applearn.databinding.ActivityRegisterBinding

class RegisterActivity : BaseActivity() {
    private lateinit var binding: ActivityRegisterBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.btnRegister.setOnClickListener {
            val username: String = binding.etUsername.text.toString().trim()
            val password: String = binding.etPassword.text.toString().trim()
            register(username, password)
        }

    }

    private fun register(username: String, password: String) {
        if (username.isEmpty()) {
            showToast("请输入账号")
            return
        }
        if (password.isEmpty()) {
            showToast("请输入密码")
            return
        }

        // 模拟发送json请求体到接收处理
        // 要发送的json请求体
        val params = mapOf(
            "username" to username,
            "password" to password
        )

        Api.config(POST, params)
            .postRequest(object : TtitCallBack {
                override fun onSuccess(response: String?) {
                    showToastAsync("注册成功：${response}")
                    Log.e("onSuccess", "登陆成功：${response}")
                    navigateTo(LoginActivity::class.java)
                }

                override fun onFailure(e: Exception) {
                    showToastAsync("注册失败：${e.message}")
                    Log.e("onFailure", "登陆失败：${e.message}")
                }

            })


    }
}